package owg.util.editor2d;

import java.lang.reflect.Constructor;
import java.util.Arrays;

import owg.util.data.KryoInstance.K;

public class ConstructorParams {
	/**The declaring class of the constructor. 
	 * <br/><br/>
	 * Kryo 3.0.0 Kryo contains a bug that prevents repeated deserialization of Constructor objects, 
	 * so we are only storing the class objects necessary to recreate it.*/
    public final Class<?> declaringType;
	/**The parameter types of the constructor. 
	 * <br/><br/>
	 * Kryo 3.0.0 contains a bug that prevents repeated deserialization of Constructor objects, 
	 * so we are only storing the class objects necessary to recreate it.*/
    public final Class<?>[] paramTypes;
    /**The parameter values that we will pass to the constructor.*/
    public final Object[] params;
    /**The index of the position argument of the constructor, -1 if none. 
	 * <br/><br/> 
     * The initial position of an object is not necessarily known at the time when the 
     * {@link ConstructorParams} object is created, so we will only remember where it should be.*/
    public final int posIndex;
    /**The index of the instance list argument of the constructor, -1 if none. 
	 * <br/><br/> 
     * The instance list object may outlive every object in the editor, 
     * and it may be a different object when loading a saved map, so we do not want to serialize it. 
     * Instead, we will only remember where it should be.*/
    public final int instanceListIndex;
    
    @K private ConstructorParams() {declaringType = null; paramTypes = null; params = null; posIndex = 0; instanceListIndex = 0;}
    
    public ConstructorParams(Constructor<?> constructor, Object[] params,
	    int posIndex, int instanceListIndex) {
	this.declaringType = constructor.getDeclaringClass();
	this.paramTypes = constructor.getParameterTypes();
	this.params = params;
	this.posIndex = posIndex;
	this.instanceListIndex = instanceListIndex;
    }
    
    @Override
    public String toString()
    {
    	return declaringType.getSimpleName()+Arrays.toString(params);
    }
    
    public Constructor<?> constructor() throws NoSuchMethodException
    {
    	return declaringType.getConstructor(paramTypes);
    }
}
